class Solution {
public:
    int minAnagramLength(string s) {
        int n = s.size();
        int cnt[26] = { 0 };
        for (auto i : s) cnt[i - 'a']++;

        auto check = [&](int len) {
            for (int i = 0; i < n; i += len) {
                int tmp[26] = { 0 };
                for (int j = 0; j < len; j++) tmp[s[i + j] - 'a']++;
                for (int j = 0; j < 26; j++) {
                    if (tmp[j] * (n / len) != cnt[j]) return false;
                }
            }
            return true;
        };
        for (int i = 1; i <= n; i++) {
            if (n % i == 0 && check(i)) return i;
        }
        return -1;
    }

};